package com.killer.common.util;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import sun.security.rsa.RSAPublicKeyImpl;

import javax.crypto.SecretKey;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;
import java.util.stream.Collectors;

public class JwtUtil {

    //有效期为
    public static final Long JWT_TTL = 3600000L;// 60 * 60*1000  一个小时
    //设置秘钥明文
    public static final String JWT_KEY = "lagou";

    /**
     * 创建token
     *
     * @param userId
     * @param subject
     * @return
     */
    public static String createJWT(Long userId, String subject) {
        long nowMillis = System.currentTimeMillis();
        long expMillis = nowMillis + JWT_TTL;
        JwtBuilder builder = Jwts.builder()
                .setId(String.valueOf(userId))//设置唯一编号
                .setSubject(subject)//设置主题  可以是JSON数据
                .setIssuedAt(new Date(nowMillis))//设置签发日期
                .setExpiration(new Date(expMillis))
                .signWith(SignatureAlgorithm.HS256, JWT_KEY);//设置签名 使用HS256算法，并设置SecretKey(字符串)
        //构建 并返回一个字符串
        return builder.compact();
    }

    /**
     * 解析token
     * @param jwt
     * @return
     * @throws Exception
     */
    public static Claims parseJWT(String jwt) throws Exception {
        String publicKey = "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnXJ1jnaF9KgQTort86PP9OAeg9cHgdwJ3Il5iGXD6bXtMmhk10XejfWZuN1E/ElZ0YVIA+i9ycpmVZsYyl3PoiCFZosCGi6a5ZvFsZ7GBsx1UTY8NdU2Ytj41tzilkPLo08rtwLcenIpU1/Adfxag3dtWJrIvHuEGkB4FtdV6BrOgLVf2bfulApmC2cvkiGQccATZcVknZmIbPqAs/NQ3oa7sKpVnUneHOaMHsj5OmhowJUB+j8fHmSYRRVMIwWJyJDMUqbFq1Wmv5IvtRu4swLHnWxeTwZe3FOM+0x9JDFp0dVNHeBXx0NDWjC7I+/b7lXYX6eYaPLcT44uq5qLtQIDAQAB-----END PUBLIC KEY-----";
                String pem = publicKey.replaceAll("\\-*BEGIN PUBLIC KEY\\-*", "").replaceAll("\\-*END PUBLIC KEY\\-*", "")
                .trim();
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return Jwts.parser().setSigningKey(keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(pem)))).parseClaimsJws(jwt).getBody();
    }

    public static void main(String[] args) throws Exception {
        System.out.println(parseJWT("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MzU4MjYxMjgsInVzZXJJZCI6MSwianRpIjoiMDQzMGZkMWItMzBkMC00ZGVhLThlYjctZWE0NzM0ZWM1N2JiIiwiY2xpZW50X2lkIjoia2lsbGVyIiwidXNlcm5hbWUiOiJraWxsZXIiLCJzY29wZSI6WyJhcHAiXX0.mmRGl7CBhebtnbBsIBmGzkygzIoLpgJy_MbGWdqtFu9Kfk0sMY8_ahA2N85KoRpU-cxctvToUXNsmef0WRLeri-IWStKpxLiA-uuKuV5b4h4yB_D1AZUJExbfT0O5txhFKKmJd326VBA8jVWodYr9vfbvhQdrMaHKk9ki708R1GuZQgasZ4RPJ2Z0EzKZd2o9ygziA4y2lidHsX40XHSiK7ZW5_mR4SVFf02Qw3uoj-Hq3x7Zq77709vBzQrJk9ZjqIWuMpSyYaR2L60ZR0UQzeN3YlWL7pkYUoOTB2CnQFSX3m4Rd7CRDxNXVthTebXcC-6-UWf4ZjjV7cIZ-t44g"));
    }
}
